using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Avon
{
    /// <summary>
    /// a rendelForm, itt peldanyositoma TermekDALt, a RendelDALt, es a TanacsadonoDAlt
    /// </summary>
    public partial class RendelForm : Form
    {
        TermekDAL m_Termek;
        RendelDAL m_Rendel;
        TanacsadonoDAL m_Tanacsadono;
        List<FullRendel> m_Rendelesek;
        /// <summary>
        /// a teljes rendelesi adatokat tartalmazzal
        /// </summary>
        public struct FullRendel
        {
            public RendelData rd;
            public String TanacsadoNev;
            public String TermekNev;
        }

        public RendelForm()
        {
            InitializeComponent();
            m_Termek = new TermekDAL();
            m_Rendel = new RendelDAL();
            m_Tanacsadono = new TanacsadonoDAL();
            m_Rendelesek = new List<FullRendel>();
        }

        private void RendelForm_Load(object sender, EventArgs e)
        {
            FilldgvTermekek(-1);
            FillcbTanacsadono();
        }
        /// <summary>
        /// fol toltom a Termekek dataGridet
        /// </summary>
        /// <param name="KategoriaID">a termek kategoriaja</param>
        private void FilldgvTermekek(int KategoriaID)
        {
            dgvTermek.Rows.Clear();

            List<TermekekData> TermekList = m_Termek.GetTermekList(-1);
            foreach (TermekekData item in TermekList)
            {
                dgvTermek.Rows.Add(item.TermekID, item.Nev, item.Alapar, item.KategoriaNev);
            }
        }
        /// <summary>
        /// fol toltoma  Tanacsadono comboBoxot, itt valaszthatom ki hogy melyik tanacsadono ad le rendelest
        /// </summary>
        private void FillcbTanacsadono()
        {
            List<TanacsadonoData> TanacsadonoList = m_Tanacsadono.GetTanacsadonoList(-1);
            cBTanacsadono.DataSource = TanacsadonoList;
            cBTanacsadono.DisplayMember = "TanacsadonoNev";
            cBTanacsadono.ValueMember = "TanacsadonoID";
        }
        /// <summary>
        /// a beszur gomb, itt szurok be adott termeket a Termekek DataGridViewbol a Rendel DataGridViewbe
        /// egy adott tanacsadono alapjan es darabszammal
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonBeszur_Click(object sender, EventArgs e)
        {
            FullRendel item = new FullRendel();
            item.rd.TanacsadonoID = Convert.ToInt32(cBTanacsadono.SelectedValue);
            item.rd.TermekID = Convert.ToInt32(dgvTermek.SelectedRows[0].Cells[0].Value);
            item.rd.Darab = Convert.ToInt32(tbTermekDb.Text);
            item.TanacsadoNev = cBTanacsadono.Text;
            item.TermekNev = dgvTermek.SelectedRows[0].Cells[1].Value.ToString();
            m_Rendelesek.Add(item);
            FillRendelDgv();
        }
        /// <summary>
        /// fol toltom a rendel DataGridViewet
        /// </summary>
        private void FillRendelDgv()
        {
            dgvRendel.Rows.Clear();
            foreach (FullRendel item in m_Rendelesek)
            {
                dgvRendel.Rows.Add(0, item.TermekNev, 
                    item.TanacsadoNev, item.rd.Darab);
            }
        }
        /// <summary>
        /// a torol gomb
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonTorol_Click(object sender, EventArgs e)
        {
            int index = dgvRendel.SelectedRows[0].Index;
            m_Rendelesek.RemoveAt(index);
            FillRendelDgv();
        }
        /// <summary>
        /// a mentes gomb
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMentes_Click(object sender, EventArgs e)
        {
            foreach (FullRendel rend in m_Rendelesek)
            {
                m_Rendel.BeszurRendeles(rend.rd.TermekID, rend.rd.TanacsadonoID,
                    rend.rd.Darab);
            }
            m_Rendelesek = new List<FullRendel>();
            dgvRendel.Rows.Clear();
        }
        /// <summary>
        /// a kilepes gomb
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}